var express = require('express');
var router = express.Router();

var { Users } = require('../utils/module')

var{
  checkSession,
  getDecrypt,
  startEncrypt
} = require('../utils')

/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});



//注册路由
router.post('/htmlEjs/register', function (req, res) {
  var body = req.body;
  // console.log(body);
  var {username,password} = body;
  if (username&&password) {//判断用户是否输入了用户名
    Users.findOne({
      username:username
    }).then(result => {
      // console.log(result);
      //
      if(result){
        res.send("<script>alert('用户名已存在');location.href='/htmlEjs/register'</script>")
      }else{
        body.time = new Date();
        // console.log(body);
        Users.insertMany(body).then(data=>{
          console.log(data);
          res.send(`<script>alert('注册成功');location.href='/htmlEjs/login?username=${startEncrypt(username)}'</script>`)
        })
      }
    })
  } else {
    res.send("<script>alert('用户名不能为空，请填写用户名');location.href='/htmlEjs/register'</script>")

  }

})


//登录路由
router.post('/htmlEjs/login', (req, res) => {
  var body = req.body;
  // console.log(body);
  // res.send(`<script>alert('denglu')</script>`)
  var {username,password} = body;
  //判断用户输入的用户名和密码是否存在
  if(username&&password){
    Users.findOne({
      username
    }).then(result=>{
      // console.log(result);
      if(result){//判断数据库有没有用户输入的数据
        if(result.password == password){
          // console.log(req.session);
            req.session.username = result.username;
            req.session.logintime = new Date();
            req.session.token = startEncrypt(result.username);
            res.redirect("/htmlEjs/index1");//重定向，登录成功就跳转首页，不会产生新的历史记录
            
        }else{
          
          res.send("<script>alert('用户名或密码错误');location.href='/htmlEjs/login'</script>")
        }

      }else{
        res.send("<script>alert('用户名不存在');location.href='/htmlEjs/login'</script>")
      }

    })

  }else{
    res.send("<script>alert('用户名或密码不能为空，请填写');location.href='/htmlEjs/login'</script>")
  }
})


//修改密码
router.post('/htmlEjs/changepass',(req,res)=>{
  checkSession(req,res,()=>{
    var body = req.body;
    console.log(body);
    var {oldpass,newpass} = body;//用户输入的数据
    console.log(oldpass,newpass);
    var username = req.session.username;
    Users.findOne({
      username
    }).then(result=>{
      console.log(result);
      if(result){
        //存在就判断旧密码是否与数据库里相同
        if(oldpass == result.password  ){
          Users.updateMany({
            username
          },{
            $set:{
              password:newpass
            }
          }).then(data=>{
            res.send("<script>alert('密码修改成功，请重新登录');location.href='/htmlEjs/login'</script>")
          })

        }else{
          res.send("<script>alert('旧密码不正确');location.href='/htmlEjs/changepass'</script>")
        }
      }
      else{
        // res.send("<script>alert('用户名不存在');location.href='/htmlEjs/changepass'</script>")
      }



    })
  })
})







// router.get('/login',function(req,res){//解析get方式的请求数据
//   // res.render('login');//渲染login页面
//   res.send('登录成功');//相当于res.write();输出到页面
// })





module.exports = router;
